/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Tools;
using Microsoft.Win32;

namespace DslEditorPowerToy.VisualStudio
{
    /// <summary>
    /// A class for automating Visual Studio tools
    /// </summary>
    public static class ToolsHelper
    {
        private const string RegKey_RegistryHive_Standard = @"Software\Microsoft\VisualStudio\8.0";
        private const string RegKey_RegistryHive_Experimental = RegKey_RegistryHive_Standard + "Exp";
        private const string VsSdkToolRelPath = @"VisualStudioIntegration\Tools\Bin";
        private const string RegKey_VsSdkInstallDirPath = @"Software\Microsoft\VisualStudio\VSIP\8.0";
        private const string RegValue_VsSdkInstallDirValue = @"InstallDir";

        #region Constructors
        static ToolsHelper()
        {
            //Register handler for loading missing assemblies
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Registers managed package assembly
        /// </summary>
        /// <param name="assemblyPath"></param>
        public static void RegisterPackage(string assemblyPath, RegistryHive hive)
        {
            //Register package in various hives
            if (0 != (hive & RegistryHive.Standard))
                RegPkg.Register(assemblyPath, RegKey_RegistryHive_Standard, RegistrationMethod.Default);
            if (0 != (hive & RegistryHive.Experimental))
                RegPkg.Register(assemblyPath, RegKey_RegistryHive_Experimental, RegistrationMethod.Default);
        }
        /// <summary>
        /// Unregisters managed package assembly
        /// </summary>
        /// <param name="assemblyPath"></param>
        public static void UnregisterPackage(string assemblyPath, RegistryHive hive)
        {
            try
            {
                //Un-register package in various hives
                if (0 != (hive & RegistryHive.Standard))
                    RegPkg.Unregister(assemblyPath, RegKey_RegistryHive_Standard, RegistrationMethod.Default);
                if (0 != (hive & RegistryHive.Experimental))
                    RegPkg.Unregister(assemblyPath, RegKey_RegistryHive_Experimental, RegistrationMethod.Default);
            }
            catch (Exception)
            {
                //Ignore and continue
            }
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Resolves missing assemblies. We need this for RegPkg.exe since its not on the standard load paths.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            AssemblyName name = new AssemblyName(args.Name);
            if (0 == string.Compare(name.Name, "RegPkg", true))
            {
                string vsSdkToolsPath = Path.Combine(GetVsSdkInstallPath(), VsSdkToolRelPath);
                string assemblyPath = Path.Combine(vsSdkToolsPath, @"regpkg.exe");

                return Assembly.LoadFrom(assemblyPath);
            }

            return null;
        }
        /// <summary>
        /// Gets the install directory for the VS SDK
        /// </summary>
        /// <returns></returns>
        private static string GetVsSdkInstallPath()
        {
            //Read value from registry
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RegKey_VsSdkInstallDirPath))
            {
                if (null == key)
                    return null;

                return (string)key.GetValue(RegValue_VsSdkInstallDirValue);
            }
        }
        #endregion
    }

    [Flags]
    public enum RegistryHive
    {
        Standard = 1,
        Experimental = 2
    }
}
